﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CharacterTrailClient.Controls;
using CharacterTrailClient.Query.Item;
using CharacterTrailLibrary.Factory;
using CharacterTrailLibrary.Interface;
using CharacterTrailLibrary.Persist.BLL.Model;
using DevExpress.XtraEditors;
using Telegnosis.Framework.UI.Util;

namespace CharacterTrailClient.Windows
{
    public partial class FormRelation : FormBase, IProcudtDetailViewControl
    {
        private readonly IProductPersist _productPersist = null;
        private readonly IBarCodeTracking _barCodeTrackingImpl = InstanceFactory.GetBarCodeTracking();
        private readonly List<TProductDetailModel> _productDetailModels= new List<TProductDetailModel>();
        public FormRelation()
        {
            _productPersist = InstanceFactory.GetProductPersist();
            InitializeComponent();
            this.productBaseEditMain.ProcudtDetailViewControl = this;
            this.gridControlProductDetailModels.DataSource = _productDetailModels;
        }

        public override string HeaderText
        {
            get
            {
                return "     串码    ";
            }
        }

        public void AppendProductDetailModel(TProductDetailModel  productDetailModel )
        {
            _productDetailModels.Add(productDetailModel);
            this.gridControlProductDetailModels.RefreshDataSource();
        }

        public void ClearProductDetailModels()
        {
            _productDetailModels.Clear();
            this.gridControlProductDetailModels.RefreshDataSource();
        }

        public void AppendProductDetailModels(List<TProductDetailModel> productDetailModels)
        {
            foreach (var productDetailModel in productDetailModels)
            {
                 _productDetailModels.Add(productDetailModel);
            }
            this.gridControlProductDetailModels.RefreshDataSource();
        }

        private void CmdQuery1_Click(object sender, EventArgs e)
        {
            try
            {
                ClearProductDetailModels();

                string condition = " 5=5 ";
                condition += this.dateItemInputQuery.GetCondition();
                AppendProductDetailModels( _productPersist.GetProductDetailModels(condition));
            
            }
            catch (Exception exception)
            {
                Message.MsgBox.ShowError(exception.Message);
            }
          
        }

        private void CmdQuery2_Click(object sender, EventArgs e)
        {
            try
            {
                string condition = " 77=77 ";

                foreach (var control in this.groupControlBase.Controls)
                {
                    if(control is IQueryItem)
                    {
                        var queryItem = (IQueryItem) control;
                        condition += queryItem.GetCondition();
                    }
                }

                this.gridControlProductBase.DataSource =
                 _productPersist.GetProductBaseModels(condition);
            }
            catch (Exception exception)
            {
                Message.MsgBox.ShowError(exception.Message);
            }
        }

        private void CmdQuery3_Click(object sender, EventArgs e)
        {
            try
            {
                string condition = " 77=77 ";

                foreach (var control in this.groupControlHeader.Controls)
                {
                    if (control is IQueryItem)
                    {
                        var queryItem = (IQueryItem)control;
                        condition += queryItem.GetCondition();
                    }
                }

                this.gridControlProductHeader.DataSource =
                 _productPersist.GetProductHeaderModels(condition);
            }
            catch (Exception exception)
            {
                Message.MsgBox.ShowError(exception.Message);
            }
        }

        private void CmdQuery4_Click(object sender, EventArgs e)
        {
            try
            {
                string condition = " 77=77 ";

                foreach (var control in this.groupControlDetail.Controls)
                {
                    if (control is IQueryItem)
                    {
                        var queryItem = (IQueryItem)control;
                        condition += queryItem.GetCondition();
                    }
                }
                this.gridControlProductDetail.DataSource=
               _barCodeTrackingImpl.GetVProductDetailHeaderBaseModelsByCondition(condition);

            }
            catch (Exception exception)
            {
                Message.MsgBox.ShowError(exception.Message);
            }
        }
    }
}